//
//  FKTableViewCellStyleSet.m
//  FKPullMenuDemo
//
//  Created by 云眼科技 on 16/7/14.
//  Copyright © 2016年 CloudEyes. All rights reserved.
//

#import "FKTableViewCellStyleSet.h"

#define kPadding 5.f

#pragma mark - --***************************** | custom the UIImageView & UILabel |  *****************************--

@implementation FKImageView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
    }
    return self;
}

-(void)commonInit{
    _isRoundRect = NO;
    self.layer.masksToBounds = YES;
    self.contentMode = UIViewContentModeScaleAspectFill;
}

-(void)setIsRoundRect:(BOOL)isRoundRect{
    _isRoundRect = isRoundRect;
    if (isRoundRect == YES) {
        self.layer.cornerRadius = self.frame.size.height/2;
        self.layer.masksToBounds = YES;

    }else{
        self.layer.cornerRadius = 0;
        self.layer.masksToBounds = YES;
    }
}

@end

@implementation FKLabel

- (instancetype)init{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}
   
- (instancetype)initWithCoder:(NSCoder *)coder{
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit]; 
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

-(void)commonInit{
    //default font
    if (self.frame.size.height > 0) {
        self.font = [UIFont systemFontOfSize:self.frame.size.height/2];
    }else{
        self.font = [UIFont systemFontOfSize:12.f];
    }
    //default textColor, 'gray color'
    self.textColor = [UIColor grayColor];
    
    self.numberOfLines = 0;
    self.lineBreakMode = NSLineBreakByWordWrapping;
}

@end



@implementation FKDefaultTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _iconView = [[FKImageView alloc] init];
        _descriptionLabel = [[FKLabel alloc] init];   
        
        [self.contentView addSubview:_iconView];
        [self.contentView addSubview:_descriptionLabel];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.iconView.frame = CGRectMake(kPadding, 
                                     kPadding,
                                     self.contentView.view_height - kPadding * 2, 
                                     self.contentView.view_height - kPadding * 2);
    
    CGRect lbFrame = CGRectMake(CGRectGetMaxX(self.iconView.frame) + kPadding,
                                kPadding, 
                                self.contentView.view_width - self.iconView.view_width - kPadding * 3,
                                self.iconView.view_height);
    self.descriptionLabel.frame = lbFrame; 
}

@end


@implementation FKTextTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _nameLabel = [[FKLabel alloc] init];
        _desLabel = [[FKLabel alloc] init];   
        _desLabel.textAlignment = NSTextAlignmentCenter;
        
        [self.contentView addSubview:_nameLabel];
        [self.contentView addSubview:_desLabel];
    }
    return self;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat width = self.contentView.view_width - kPadding * 6;
    self.nameLabel.frame = CGRectMake(kPadding * 4, kPadding, width * 4/5, self.contentView.view_height - kPadding * 2);
    
    CGFloat lbh = self.contentView.view_height - kPadding * 2;
    self.desLabel.frame = CGRectMake(CGRectGetMaxX(self.nameLabel.frame) + kPadding, kPadding , width/5, lbh);
    self.desLabel.layer.cornerRadius = (lbh) / 2;
    self.desLabel.layer.masksToBounds = YES;
    self.desLabel.backgroundColor = [UIColor grayColor];
    self.desLabel.textColor = [UIColor whiteColor];
    self.desLabel.font = [UIFont boldSystemFontOfSize:lbh/2];
}


@end



#pragma mark - ========================= DownloadCell =========================

@implementation FKDownloadTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self commonInit];
    }
    return self;
}

-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat cellWidth = self.contentView.frame.size.width;
    CGFloat cellHeight = self.contentView.frame.size.height;
    CGFloat padding = 5.0f;
    
    CGFloat icon_h = cellHeight - padding * 4.f;
    self.iconView.frame = CGRectMake(padding, padding * 2, icon_h, icon_h);
    
    CGFloat nameLabel_x = CGRectGetMaxX(self.iconView.frame) + 5;
    CGFloat nameLabel_w = cellWidth - padding * 4.f - icon_h * 2;
    CGFloat nameLabel_h = cellHeight / 5; 
    self.nameLabel.frame     = CGRectMake(nameLabel_x, 
                                          padding, 
                                          nameLabel_w, 
                                          nameLabel_h);
    
    self.sizeLabel.frame     = CGRectMake(nameLabel_x, 
                                          CGRectGetMaxY(self.nameLabel.frame), 
                                          nameLabel_w,
                                          nameLabel_h);
    
    self.speedLabel.frame    = CGRectMake(nameLabel_x,
                                          CGRectGetMaxY(self.sizeLabel.frame),
                                          nameLabel_w, 
                                          nameLabel_h);
    
    self.leftTimeLabel.frame = CGRectMake(nameLabel_x, 
                                          CGRectGetMaxY(self.speedLabel.frame),
                                          nameLabel_w, 
                                          nameLabel_h);
    
    self.progressView.frame  = CGRectMake(nameLabel_x,
                                          CGRectGetMaxY(self.leftTimeLabel.frame) + 3.f,
                                          nameLabel_w, 
                                          nameLabel_h);
    
    self.downloadBtn.frame =  CGRectMake(CGRectGetMaxX(self.nameLabel.frame) + padding * 2, 
                                         (cellHeight - icon_h/2)/2,
                                         icon_h - padding, 
                                         icon_h/2);
}



-(void)commonInit{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    _iconView = [[UIImageView alloc] init];
    _iconView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:_iconView];
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = [UIFont systemFontOfSize:13.f];
    _nameLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_nameLabel];
    
    _sizeLabel = [[UILabel alloc] init];
    _sizeLabel.font = [UIFont systemFontOfSize:12.f];
    _sizeLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_sizeLabel];
    
    _speedLabel = [[UILabel alloc] init];
    _speedLabel.font = [UIFont systemFontOfSize:12.f];
    _speedLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_speedLabel];
    
    _leftTimeLabel = [[UILabel alloc] init];
    _leftTimeLabel.font = [UIFont systemFontOfSize:12.f];
    _leftTimeLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_leftTimeLabel];
    
    _progressView = [[UIProgressView alloc] init];
    _progressView.progressTintColor = [UIColor blackColor];
    _progressView.trackTintColor = [UIColor lightGrayColor];
    _progressView.progress = 0.0f;
    [self.contentView addSubview:_progressView];
    
    _downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _downloadBtn.layer.cornerRadius = 6.f;
    _downloadBtn.layer.borderColor = [UIColor grayColor].CGColor;
    _downloadBtn.layer.borderWidth = 0.5f;
    _downloadBtn.titleLabel.font = [UIFont systemFontOfSize:12];  
    [_downloadBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_downloadBtn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_downloadBtn];
}

-(void)btnPressed:(UIButton *)sender{
    if (self.delegate &&[self.delegate respondsToSelector:@selector(FKDownloadCellDidPressedButtonForIndexPath:)]) {
        [self.delegate FKDownloadCellDidPressedButtonForIndexPath:self.indexPath];
    }
}

@end






@implementation FKTableViewCellStyleSet
@end
